<template>
    <div >
        <div>
            <Card title="试卷管理"></Card>
         <Table   stripe border :columns="columns" :data="data1" ref="selection"  @on-selection-change="selectList"></Table>
        <!-- 分页处理 -->
        
         <!-- 按钮功能 -->
         <Button @click="handleSelectAll(true)" size="large">全选</Button>
         <Button @click="handleSelectAll(false)" size="large">取消全选</Button>
         <Button @click="del" size="large">删除选中项</Button>
           <Button  size="large" @click="exportData"><Icon type="ios-download-outline"></Icon>导出数据</Button>

       <!--   <Button @click="add">添加数据</Button> -->
    </div>
    </div>
</template>
<script>
export default {
  data() {
    return {
      data1:[],
      teacherExampagesMsg:[],
      selectIds: [],
      get:false,
      disabled: false,
      columns: [
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "试卷题目",
          key: "examPageTitle"
        },
         {
          title: "试卷类型",
          key: "testType"
        },
        {
          title: "试卷信息",
          key: "examInfo"
        },
        {
          title: "考试开始时间",
          key: "examStartTime"
        },
        {
          title: "考试时长",
          key: "examTime"
        },

        {
          title: "出题人",
          key: "setter"
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.show(params.index);
                    }
                  }
                },
                "视图"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.remove(params.index);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
    };
  },
  created() {
      const teachername = sessionStorage.getItem('teacherName');
      console.log(teachername)
      var Msg = {setter:teachername};
     this.$axios({
      method: "post",
      url: "http://"+this.urlpath+"/FindTeacherExamMsg",
      data:Msg 
    }).then(res => {
      this.data1 = res.data;
      console.log(this.data1)
      this.get=true
    });
  },
  
   mounted() {
   
   },

  methods: {
    show(index) {
      this.$Modal.info({
        title: "试卷题目",
        content: `年级：${this.data1[index].examPageTitle}<br>试卷类型：${
          this.data1[index].testType
        }<br>试卷信息：${this.data1[index].examInfo}
        <br>考试开始时间：${this.data1[index].examStartTime} <br>
        考试时长：${this.data1[index].examTime} <br>
        出题人：${this.data1[index].setter}`
      });
    },
    remove(index) {
      var idone = [];
      idone.push(this.data1[index]._id);
      this.data1.splice(index, 1);
      this.$axios({
        method: "post",
        url: "http://"+this.urlpath+"/RemoveExamPage",
        data: idone
      }).then(res => {});
    },
    handleSelectAll(status) {
      this.$refs.selection.selectAll(status);
    },
      exportData(){
      this.$refs.selection.exportCsv({
                        filename: '考卷信息'
                    });
    },

    selectList(selection) {
      if (selection.length > 0) {
        this.disabled = false;
        this.selectIds = []; /* 把上次点击的去掉，否则会叠加 */
        for (let i = 0; i < selection.length; i++) {
          this.selectIds.push(selection[i]._id);
        }
      } else {
        this.disabled = true;
      }
    },
    del() {
      for (let i = 0; i < this.selectIds.length; i++) {
        var deleteId = this.data1.findIndex(item => {
          return item._id === this.selectIds[i];
        });
        this.data1.splice(deleteId, 1);
      }

      this.$axios({
        method: "post",
        url: "http://"+this.urlpath+"/RemoveExamPage",
        data:this.selectIds
      }).then(res => {});
    }
  }
};
</script>
<style scoped>
</style>